<?php
class FollowService extends Service {
	//所有设置的值
	var $opt	=	array();
	var $group	=	'';
	var $model;
	var $count;
	var $mid;
	
	/**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @author melec制作
     * @access public
     +----------------------------------------------------------
     */
	public function __construct() {
		//1.判断是否安装、是否运行该服务，系统服务可以不做判断
		//2.服务初始化
		$this->init();
		$this->run();
	}

	//服务初始化
	public function init(){
		$this->model = D('Follow');
		$this->count = D('FollowCount');
		$this->mid   = $_SESSION['mid'];
		
		if($this->group==''){
			$this->group     =   'follow';
		}
	}

	//运行服务，系统服务自动运行
	public function run(){
//		//获取本组所有配置
//		//todo：缓存
//		$options	=	D('Option')->where("appname='$this->group'")->findAll();
//		foreach((array) $options as $v){
//			$this->opt[$v['name']]	=	$v['value'];
//		}
	}
	
	function addFollowData($data,$type='sort',$mid=''){
		//SamPeng 修改
		//如果为数组
		if(is_array($data)){
			$result = array();
			foreach ($data as $v){
				$result[] = $this->addFollowData($v,$type,$mid);
			}
			return count(array_filter($result)) == count($result);
		}else{
			//如果不是数组;
			return $this->_addData($data,$type,$mid);
		}

	
	}
	
	private function _addData($data,$type='sort',$mid){
		if(empty($data)){
			return false;
		}
		
		$map['uid']  = ($mid!='')?$mid:$this->mid;
		$map['type'] = $type;	
			
		if($type=='user') $map['aimId'] = $data;
		$map['data'] = $data;
		
		$id = $this->model->where($map)->getField('id');
		if($id){
				return false;
		}

		
		return  $this->model->add($map);
	}
	
	function delFollowData($data,$type='sort'){
		$map['uid']  = $this->mid;
		$map['type'] = $type;		
		if($type=='user') $map['aimId'] = $data;
		else              $map['data'] = $data;
		
		return $this->model->where($map)->delete();
	}	
	
	function getFollowData($uid='',$type='sort'){
		if(empty($uid)){
			$uid = $this->mid;
		}
		
		$map['uid']  = $uid;
		$map['type'] = $type;
		
		if($type=='user') $field = 'aimId';
		else              $field = 'data';
				
		$res = $this->model->where($map)->field($field)->findAll();
		foreach($res as $v){
			$result[] = $v[$field];
		}
		return $result;
	}
	
	function isFollow($data,$type='sort'){
		$map['uid']  = $this->mid;
		$map['type'] = $type;		
		if($type=='user') $map['aimId'] = $data;
		else              $map['data'] = $data;
		
		$res = $this->model->where($map)->find();
		//dump($this->model->getLastSql());	
		if($res){
			return true;
		}
		
		return false;
	}
	//关注目标的数目
	function getDataCount($data,$type='sort'){
		$map['type'] = $type;		
		if($type=='user') $map['aimId'] = $data;
		else              $map['data'] = $data;
		
		return $this->model->where($map)->count();
	}
	//用户关注的数目
	function getUserCount($uid,$type='sort'){
		$map['type'] = $type;		
		$map['uid']  = $uid;
		
		return $this->model->where($map)->count();
	}
	
	function getFollow($map,$field='*',$pageRow=0,$order='id desc'){
		if($pageRow){
			return $this->model->where($map)->field($field)->order($order)->findPage($pageRow);
		}
		return $this->model->where($map)->field($field)->order($order)->findAll();
	}
		
	function initCount($type,$uid=''){
		if(empty($uid)){
			$uid = $this->mid;
		}

		$map['uid'] = $uid;
		$map['type'] = $type;
		$res = $this->count->where($map)->find();
		if($res){
			return true;
		}
		
		$map['lastTime'] = time();
		$map['count']    = 0;
		return $this->count->add($map);
	}
			
	function getCount($type,$uid=''){
		if(empty($uid)){
			$uid = $this->mid;
		}
		
		$map['uid'] = $uid;
		$map['type'] = $type;
		$count = $this->count->where($map)->getField('count');
		
		return $count ? $count : 0;
	}
	
	function setCount($count,$type,$uid=''){
		if(empty($uid)){
			$uid = $this->mid;
		}
		
		$map['uid'] = $uid;
		$map['type'] = $type;
		return $this->count->where($map)->setField('count',$count);
	}	
		
	function clearCount($type,$uid=''){
		if(empty($uid)){
			$uid = $this->mid;
		}

		$data['lastTime'] = time();
		$data['count']    = 0;
		
		$map['uid'] = $uid;
		$map['type'] = $type;
		return $this->count->where($map)->save($data);		
	}



	/* 后台管理相关方法 */

	//启动服务，未编码
	public function _start(){
		return true;
	}

	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//卸载服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>